﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TypedROBIN.Imaging;
using System.IO;

namespace IECaptRemotingClient
{
    class Program
    {
        static void Main(string[] args)
        {
            string outputDir = Directory.GetParent(System.Environment.CurrentDirectory).Parent.Parent.Parent.FullName + @"\temp\";
            try
            {
                Random ran = new Random((int)DateTime.Now.Ticks);
                int clientPort = ran.Next(8182, 8888);
                IIECaptEngine pageSnaprFactory = RemotingRegister.GetRemotingProxyByCode("localhost", 8181, clientPort);
                IIECaptr pageSnapr = (IIECaptr) pageSnaprFactory.GetIECaptr();
                Console.WriteLine("GetRemotingProxyByCode");
                Console.WriteLine("clientPort = " + clientPort.ToString());

                EventWrapper ew = new EventWrapper();
                
                pageSnapr.OnIECaptComplete += new CompleteEventHandler(ew.LocallyCompleteEventHandled);
                ew.LocalCompleteEvent += new CompleteEventHandler(pageSnapr_OnSnaprComplete);

                pageSnapr.OnIECaptError += new TypedROBIN.Imaging.ErrorEventHandler(ew.LocallyErrorEventHandled);
                ew.LocalErrorEvent += new TypedROBIN.Imaging.ErrorEventHandler(pageSnapr_OnSnaprError);

                pageSnapr.OnIECaptStatus += new TypedROBIN.Imaging.StatusEventHandler(ew.LocallyStatusEventHandled);
                ew.LocalStatusEvent += new TypedROBIN.Imaging.StatusEventHandler(pageSnapr_OnSnaprStatus);

                Arguments arguments = new Arguments(
                    "http://tw.yahoo.com/", 
                    outputDir + clientPort.ToString() + @"yahoo1024.png",
                    "ROBIN-" + clientPort.ToString(),
                    1024);
                Console.WriteLine("Task Id = " + pageSnapr.Capture(arguments));
                //Console.WriteLine("Capture Done!");
            }
            catch(Exception E)
            {
                Console.WriteLine(E.ToString());
            }
            Console.ReadLine();
        }

        static void pageSnapr_OnSnaprStatus(object sender, IECaptStatusEventArgs e)
        {
            Console.WriteLine(e.Status);
        }

        static void pageSnapr_OnSnaprError(object sender, IECaptErrorEventArgs e)
        {
            Console.WriteLine("\t錯誤>>> " + e.StatusCode + " -- " + e.Message );
        }

        static void pageSnapr_OnSnaprComplete(object sender, IECaptCompleteEventArgs e)
        {
            Console.WriteLine("\t完成>>>");
            Console.WriteLine("\t\t識別: " + e.TaskId);
            Console.WriteLine("\t\t網頁: " + e.OrignalArguments.Url);
            Console.WriteLine("\t\t檔案: " + e.OrignalArguments.OutputPath);
            Console.WriteLine("\t\t大小: " + e.SnapSize + " 位元組");
            Console.WriteLine("\t\t延遲: " + e.OrignalArguments.Delay + " 毫秒");
            Console.WriteLine("\t\t執行: " + e.ExecutionTime + " 毫秒");
            Environment.Exit(0);
        }
    }
}
